package com.cjh.lms.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.cjh.lms.exception.EncryptException;
import com.cjh.lms.service.AppService;
import com.cjh.lms.tool.RSA.RSAUtil;

@Controller
public class AppController {
	public final static Boolean isDecrypt = false;
	
	@Autowired
	AppService appService;

	@RequestMapping("/appEnterLock")
	@ResponseBody
	public Object appEnterLock(String data) throws EncryptException {
		String aesKey = "65DA3C779D47AD64";
		JSONObject info = JSONObject.parseObject(data);
		String encryptString = info.getString("encrypt");
		if (isDecrypt) {
			try {
				encryptString = RSAUtil.decryptString(RSAUtil.getPrivateKey(com.cjh.lms.tool.RSA.RSAContent.privateKey),
						encryptString);
			} catch (Exception e) {
				// TODO Auto-generated catch block.
				RuntimeException aException = new RuntimeException();
				throw aException;
			}
		}
		JSONObject encryptObject = JSONObject.parseObject(encryptString);
		JSONObject dataObject = encryptObject.getJSONObject("data");
		String lockNo = dataObject.getString("lockNo");
		String cipher = dataObject.getString("cipher");
		return appService.appEnterLock(lockNo, cipher).toJsonString(aesKey);
	}
	
	@RequestMapping("rollBackData")
	@ResponseBody
	public Object rollBackData(String data) throws EncryptException{
		String aesKey = "65DA3C779D47AD64";
		JSONObject info = JSONObject.parseObject(data);
		String encryptString = info.getString("encrypt");
		if (isDecrypt) {
			try {
				encryptString = RSAUtil.decryptString(RSAUtil.getPrivateKey(com.cjh.lms.tool.RSA.RSAContent.privateKey),
						encryptString);
			} catch (Exception e) {
				// TODO Auto-generated catch block.
				RuntimeException aException = new RuntimeException();
				throw aException;
			}
		}
		JSONObject encryptObject = JSONObject.parseObject(encryptString);
		JSONObject dataObject = encryptObject.getJSONObject("data");
		String lockNo = dataObject.getString("lockNo");
		String cipher = dataObject.getString("cipher");
		return appService.rollBackData(lockNo, cipher).toJsonString(aesKey);
	}
	
}
